package org.openkanban.client.ui.adminpanel;

import org.openkanban.client.ui.presenter.PreparedRequest;
import org.openkanban.client.ui.presenter.UpdateUserAccountPasswordRequestCallback;
import org.openkanban.client.util.Md5Utils;

import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.HeaderItem;
import com.smartgwt.client.widgets.form.fields.PasswordItem;
import com.smartgwt.client.widgets.form.fields.StaticTextItem;
import com.smartgwt.client.widgets.form.validator.MatchesFieldValidator;

/**
 * GUI Element for the UserAccountPasswordForm
 */
public class UserAccountPasswordForm extends DynamicForm {

    public static final String TARGET = "uploadTarget";

    private UpdateUserAccountPasswordRequestCallback updateUserPasswordReqCallback;
    private PasswordItem oldPassword;
    private PasswordItem password;
    private PasswordItem password2;
    private StaticTextItem message;

    public UserAccountPasswordForm(int width) {
	updateUserPasswordReqCallback = new UpdateUserAccountPasswordRequestCallback(this);

	MatchesFieldValidator validator = new MatchesFieldValidator();
	validator.setOtherField("newPassword2");
	validator.setErrorMessage("Passwörter stimmen nicht überein");

	oldPassword = new PasswordItem();
	oldPassword.setName("oldPassword");
	oldPassword.setTitle("old password");
	oldPassword.setRequired(true);

	password = new PasswordItem();
	password.setName("newPassword");
	password.setTitle("new password");
	password.setRequired(true);
	password.setValidators(validator);

	password2 = new PasswordItem();
	password2.setName("newPassword2");
	password2.setTitle("new password check");
	password2.setRequired(true);

	HeaderItem headerItem = new HeaderItem("userAccountHeaderItem", "account data");
	headerItem.setValue("user password");

	message = new StaticTextItem();
	message.setTitle("Status:");
	message.setTextAlign(Alignment.LEFT);
	message.setVisible(false);

	setWidth(width);
	setMargin(10);
	setColWidths("40%", "60%");

	setFields(headerItem, oldPassword, password, password2, message);
    }

    @Override
    public void submit() {
	if (validate()) {
	    PreparedRequest prepReq = new PreparedRequest("user", "update/password", "oldPassword", Md5Utils.md5(oldPassword
		    .getValueAsString()), "newPassword", Md5Utils.md5(password.getValueAsString()));
	    prepReq.sendAsyncRequest(updateUserPasswordReqCallback);
	}
    }

    public void clearView() {
	oldPassword.clearValue();
	password.clearValue();
	password2.clearValue();
    }

    public void showSuccessMessage() {
	clearView();

	message.setValue("update successful!");
	message.setTextBoxStyle("successMessage");
	message.updateState();
	message.show();
    }

    public void showFailureMessage() {
	clearView();

	message.setValue("update failed!");
	message.setTextBoxStyle("failedMessage");
	message.updateState();
	message.show();
    }

}
